﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ReporterApplication
{
    public class rSelectorNode
    {
        public string SelectorText { get; set; }
        public rSelectorNode Next { get; set; }

        public rSelectorNode Filter { get; set; }

        public rSelectorNode Parent { get; set; }

        public rQuery Query(rQuery rquery)
        {
            var pureSelectorText = SelectorText.Substring(1,SelectorText.Length - 1);

            var result = new rQuery();
            
            if (SelectorText.StartsWith("#"))
            {
                result = rquery.GetByProperty(pureSelectorText);                
            }
            
            
            if (Next != null)
            {
                return Next.Query(result);
            }

            return result;
        }

        public virtual void Info(StringBuilder sb, int indend)
        {
            sb.Append(" ".PadLeft(indend));
            sb.AppendLine(SelectorText);

            indend+=2;

            if (Filter != null)
            {
                sb.Append(" ".PadLeft(indend));
                sb.AppendLine("Filter:");
                sb.Append(" ".PadLeft(indend));

                Filter.Info(sb, indend);
            }

            indend -= 2;

            if(Next != null)
                Next.Info(sb, indend);
        }

        public static rSelectorNode NewNode(rSelectorNode node)
        {
            if(string.IsNullOrEmpty(node.SelectorText))
                return node;

            return new rSelectorNode();
        }

        public static rSelectorNode NewNextNode(rSelectorNode node)
        {
            if (string.IsNullOrEmpty(node.SelectorText))
                return node;
            
            var newNode = new rSelectorNode {Parent = node };
            node.Next = newNode;
            return newNode;
        }
    }

    public class rExpNode : rSelectorNode
    {
        public string expString { get; set; }
        public rSelectorNode Left { get; set; }
        public rSelectorNode Right { get; set; }

        public override void Info(StringBuilder sb, int indend)
        {
            indend+=2;

            sb.Append(" ".PadLeft(indend));
            sb.AppendLine(Left.SelectorText + SelectorText + Right.SelectorText);

            indend += 2;

            if (Left != null)
                Left.Info(sb, indend);

            sb.Append(" ".PadLeft(indend));
            sb.AppendLine("==");

            if (Right != null)
                Right.Info(sb, indend);
        }
    }

    public class rMethodNode : rSelectorNode
    {
        public rSelectorNode Parameters { get; set; }

        public override void Info(StringBuilder sb, int indend)
        {
            indend += 2;

            sb.Append(" ".PadLeft(indend));
            sb.AppendLine(SelectorText);

            indend += 2;

            if (Parameters != null)
            {
                sb.AppendLine("Parameters:");

                sb.Append(" ".PadLeft(indend));

                Parameters.Info(sb, indend);
            }

            indend -= 2;

            if (Next != null)
                Next.Info(sb, indend);
        }
    }
}
